﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Core;
using SmartSapce.ServiceLocation;

namespace SmartSpace.WebApp._console.core
{
    public partial class application_navigation_add : System.Web.UI.Page
    {
        private Guid _parentId;
        private Guid _applicationId;

        protected void Page_Load(object sender, EventArgs e)
        {
            _applicationId = new Guid(Request.QueryString["applicationid"]);
            if(!string.IsNullOrEmpty(Request.QueryString["parentid"]))
            {
                _parentId = new Guid(Request.QueryString["parentid"]);
            }

            if (!IsPostBack)
            {
                ICoreService service = ServiceLocator.Get<ICoreService>();

                List<Module> modules = service.GetModules(_applicationId);
                foreach (var item in modules)
                {
                    BindModule(_ctlTreeView, item);
                }
                _ctlTreeView.ExpandAll();

                if (_parentId == Guid.Empty)
                {
                    _ctlEditParent.Text = "无";
                }
                else
                {
                    _ctlEditParent.Text = service.GetNavigation(_parentId).Caption;
                }
            }

            if (_parentId == Guid.Empty)
            {
                _ctlLinkBack.NavigateUrl = string.Format("./application-navigation-list.aspx?id={0}", _applicationId.ToString("D"));
            }
            else
            {
                _ctlLinkBack.NavigateUrl = string.Format("./application-navigation-list.aspx?id={0}&navigationid={1}", _applicationId.ToString("D"), _parentId.ToString("D"));
            }
        }

        protected void _ctlReset_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("./application-navigation-add.aspx?applicationid={0}", _applicationId.ToString("D")));
        }

        protected void _ctlBtnSave_Click(object sender, EventArgs e)
        {
            Navigation navigation = new Navigation();
            navigation.AppId = _applicationId;
            navigation.Id = Guid.NewGuid();
            navigation.Caption = _ctlEditCaption.Text;
            navigation.Description = _ctlEditDescription.Text;
            navigation.GorupName = _ctlEditGroupName.Text;
            navigation.Arguments = _ctlEditArguments.Text;
            
            navigation.ParentId = _parentId;

            if (radio_other.Checked)
            {
                navigation.UrlType = "VIRTUALPAGE";
                navigation.Url =  _ctlEditUrl.Text;
            }
            else
            {
                if (_ctlTreeView.CheckedNodes.Count > 0)
                {
                    TreeNode selectedViewNode = _ctlTreeView.CheckedNodes[0];
                    TreeNode selectedModuleNode = selectedViewNode.Parent;
                    //navigation.ModuleId = new Guid(selectedModuleNode.Value);

                    navigation.UrlType = "VIEW";
                    navigation.Url = selectedViewNode.Value;
                }
            }

            ICoreService service = ServiceLocator.Get<ICoreService>();
            service.SaveNavigation(navigation);
            Response.Redirect(_ctlLinkBack.NavigateUrl);
        }

        #region bind module - view
        private void BindModule(TreeView tree, Module module)
        {
            TreeNode node = new TreeNode();
            node.Text = module.Caption;
            node.Value = module.Id.ToString("D");
            node.ShowCheckBox = false;
            tree.Nodes.Add(node);
            foreach (var item in module.Views)
            {
                BindModuleView(node, item);
            }
        }
        private void BindModuleView(TreeNode tree, ModuleView view)
        {
            TreeNode node = new TreeNode();
            node.Text = view.Caption;
            node.Value = view.Id.ToString("D");
            node.ShowCheckBox = true;
            tree.ChildNodes.Add(node);
        }
        #endregion
    }
}